var board = {0:0,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0}
var hasConflicted = new Array()
var score = 0

var my_mark = {2:2,4:4,8:8,16:16,32:32,64:64,128:128,256:256,512:512,1024:1024}
var my_list = {0:0,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0}
var app = new Vue({
	el: '#game',
	data: {
		my_list: my_list,
		score:score,
		board:board
	},
	mounted: function () {
		ResetPos()
		this.Init()
	},
	methods:{
		Init:function(){
			//初始化数据
			for(i in this.board){
				this.board[i] = 0
			}
			this.score = 100
			this.generateOneNumber()
			this.generateOneNumber()
		},
		//随机位置出现数字
		generateOneNumber: function(){
		    if( nospace( this.board ) )
		        return false;
		
		    //随机一个位置
		    var rand = parseInt( Math.floor( Math.random()  * 16 ) );
		    var times = 0;
		    while( times < 50 ){
		        if( this.board[rand] == 0 ){
		        	break;
		        } 
		        rand = parseInt( Math.floor( Math.random()  * 16 ) ); 		
		        times ++;
		    }
		    if( times == 50 ){
		        for(var i=0;i<16;i++){
		        	board[i] == 0
		        	rand = i
		        	break
		        }
		    }
		    //随机一个数字
		    var randNumber = Math.random() < 0.5 ? 2 : 4;
		    //在随机位置显示随机数字
		    this.board[rand] = randNumber;
//		    showNumberWithAnimation( randx , randy , randNumber );
		    return true;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
})

//随机位置出现数字
function generateOneNumber(){
		    if( nospace(board ) )
		        return false;
		
		    //随机一个位置
		    var rand = parseInt( Math.floor( Math.random()  * 16 ) );
		    var times = 0;
		    while( times < 50 ){
		        if( board[rand] == 0 ){
		        	break;
		        } 
		        rand = parseInt( Math.floor( Math.random()  * 16 ) ); 		
		        times ++;
		    }
		    if( times == 50 ){
		        for(var i=0;i<16;i++){
		        	board[i] == 0
		        	rand = i
		        	break
		        }
		    }
		    //随机一个数字
		    var randNumber = Math.random() < 0.5 ? 2 : 4;
		    //在随机位置显示随机数字
		    board[rand] = randNumber;
//		    showNumberWithAnimation( randx , randy , randNumber );
		    return true;
		}
//移动数字
$(document).keydown( function( event ){
    switch( event.keyCode ){
        case 37: //left
            if( moveLeft() ){
                setTimeout("generateOneNumber()",100);
//              setTimeout("isgameover()",300);
            }
            break;
        case 38: //up
            if( moveUp() ){
                setTimeout("generateOneNumber()",210);
//              setTimeout("isgameover()",300);
            }
            break;
        case 39: //right
            if( moveRight() ){
                setTimeout("generateOneNumber()",210);
//              setTimeout("isgameover()",300);
            }
            break;
        case 40: //down
            if( moveDown() ){
                setTimeout("generateOneNumber()",210);
//              setTimeout("isgameover()",300);
            }
            break;
        default: //default
            break;
    }
});

//向左移动
function moveLeft(){
//  if( !canMoveLeft( board ) )
//      return false;
    //moveLeft
    for( var i = 0 ; i < 4 ; i ++ )
        for( var j = 1 ; j < 4 ; j ++ ){
            if( board[i][j] != 0 ){
                for( var k = 0 ; k < j ; k ++ ){
                    if( board[i*4+k] == 0 && noBlockHorizontal( i , k , j , board ) ){
                        //move
                        showMoveAnimation( i , j , i , k );
//                      setTimeout(function(){
//                      	board[i*4+k] = board[i*4+j];
//                     		board[i*4+j] = 0;
//                     	 	
//                      },210)
                        board[i*4+k] = board[i*4+j];
                       	board[i*4+j] = 0;
                        continue;
                    }
                    else if( board[i*4+k] == board[i*4+j] && noBlockHorizontal( i , k , j , board )  ){
                        //move
                        showMoveAnimation( i , j , i , k );
                        //add
                        board[i*4+k] += board[i*4+j];
                        board[i*4+j] = 0;
                        //add score
                        score += board[i*4+k];                      

//                      hasConflicted[i][k] = true;
                        continue;
                    }
                }
            }
        }

//  setTimeout("updateBoardView()",200);
    return true;
}


function moveRight(){
//  if( !canMoveRight( board ) )
//      return false;

    //moveRight
    for( var i = 0 ; i < 4 ; i ++ )
        for( var j = 2 ; j >= 0 ; j -- ){
            if( board[i][j] != 0 ){
                for( var k = 3 ; k > j ; k -- ){

                   if( board[i*4+k] == 0 && noBlockHorizontal( i , j , k , board ) ){
                        //move
                        showMoveAnimation( i , j , i , k );
//                      setTimeout(function(){
//                      	board[i*4+k] = board[i*4+j];
//                     		board[i*4+j] = 0;
//                     	 	
//                      },210)
                        board[i*4+k] = board[i*4+j];
                       	board[i*4+j] = 0;
                        continue;
                    }
                    else if( board[i*4+k] == board[i*4+j] && noBlockHorizontal( i , j , k , board )  ){
                        //move
                        showMoveAnimation( i , j , i , k );
                        //add
                        board[i*4+k] += board[i*4+j];
                        board[i*4+j] = 0;
                        //add score
                        score += board[i*4+k];                      

//                      hasConflicted[i][k] = true;
                        continue;
                    }
                }
            }
        }

    setTimeout("updateBoardView()",200);
    return true;
}

function moveUp(){

//  if( !canMoveUp( board ) )
//      return false;

    //moveUp
    for( var j = 0 ; j < 4 ; j ++ )
        for( var i = 1 ; i < 4 ; i ++ ){
            if( board[i][j] != 0 ){
                for( var k = 0 ; k < i ; k ++ ){

                    if( board[k*4+j] == 0 && noBlockVertical( j , k , i , board ) ){
                        //move
                        showMoveAnimation( i , j , k , j );
                        board[k*4+j] = board[i*4+j];
                        board[i*4+j] = 0;
                        continue;
                    }
                    else if( board[k*4+j] == board[i*4+j] && noBlockVertical( j , k , i , board )  ){
                        //move
                        showMoveAnimation( i , j , k , j );
                        //add
                        board[k*4+j] += board[i*4+j];
                        board[i*4+j] = 0;
                        //add score
                        score += board[k*4+j];

//                      hasConflicted[k][j] = true;
                        continue;
                    }
                }
            }
        }

//  setTimeout("updateBoardView()",200);
    return true;
}

function moveDown(){
//  if( !canMoveDown( board ) )
//      return false;

    //moveDown
    for( var j = 0 ; j < 4 ; j ++ )
        for( var i = 2 ; i >= 0 ; i -- ){
            if( board[i][j] != 0 ){
                for( var k = 3 ; k > i ; k -- ){

                    if( board[k*4+j] == 0 && noBlockVertical( j , i , k , board ) ){
                        //move
                        showMoveAnimation( i , j , k , j );
                        board[k*4+j] = board[i*4+j];
                        board[i*4+j] = 0;
                        continue;
                    }
                    else if( board[k*4+j] == board[i*4+j] && noBlockVertical( j , i , k , board )  ){
                        //move
                        showMoveAnimation( i , j , k , j );
                        //add
                        board[k*4+j] += board[i*4+j];
                        board[i*4+j] = 0;
                        //add score
                        score += board[k*4+j];                      

//                      hasConflicted[k][j] = true;
                        continue;
                    }
                }
            }
        }

    setTimeout("updateBoardView()",200);
    return true;
}